//
//  HLHomeListCell.m
//  YueYue
//
//  Created by hulu001 on 2023/5/17.
//

#import "HLHomeListCell.h"
#import "HLHomeListImageView.h"

@interface HLHomeListCell ()

@property (nonatomic, strong) UIImageView *avatarView;
@property (nonatomic, strong) UIView *avatarBackView;
@property (nonatomic, strong) QMUIButton *videoButton;
@property (nonatomic, strong) UILabel *nicknameLabel;
@property (nonatomic, strong) UILabel *infoLabel;
@property (nonatomic, strong) UILabel *signLabel;
@property (nonatomic, strong) HLHomeListImageView *imagesView;
@property (nonatomic, strong) QMUIButton *greetButton;
@property (nonatomic, strong) QMUIButton *chatButton;

@property (nonatomic, strong) UIView *lineView;
@end

@implementation HLHomeListCell


- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.contentView.backgroundColor = UIColorWhite;
        [self setupUI];
    }
    return self;
}


- (void)setupUI
{
    self.avatarBackView = [[UIView alloc] init];
    _avatarBackView.layer.cornerRadius = 24;
    _avatarBackView.layer.masksToBounds = YES;
    _avatarBackView.layer.borderColor = HLMainColor.CGColor;
    _avatarBackView.layer.borderWidth = 1;
    [self.contentView addSubview:_avatarBackView];
    
    self.avatarView = [[UIImageView alloc] init];
    _avatarView.layer.cornerRadius = 20;
    _avatarView.layer.masksToBounds = YES;
    _avatarView.layer.borderColor = HLMainColor.CGColor;
    _avatarView.layer.borderWidth = 1;
    [self.contentView addSubview:_avatarView];
    [_avatarView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(40);
        make.left.mas_equalTo(12);
        make.top.mas_equalTo(12);
    }];
    
    [_avatarBackView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(_avatarView);
        make.size.mas_equalTo(48);
    }];
    
    self.videoButton = [[QMUIButton alloc] init];
    [_videoButton setTitle:@"可视频" forState:UIControlStateNormal];
    [_videoButton setTitleColor:UIColorWhite forState:UIControlStateNormal];
    [_videoButton setBackgroundColor:HLMainColor];
    _videoButton.cornerRadius = 8;
    _videoButton.contentEdgeInsets = UIEdgeInsetsMake(2, 6, 2, 6);
    _videoButton.titleLabel.font = UIFontMake(10);
    [_videoButton addTarget:self action:@selector(videoAction) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:_videoButton];
    [_videoButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(_avatarView);
        make.top.equalTo(_avatarView.mas_bottom).offset(-4);
    }];
    
    
    self.nicknameLabel = [[UILabel alloc] init];
    _nicknameLabel.textColor = HLTextColor1;
    _nicknameLabel.text = @"聊人生阿姨有没有啊";
    _nicknameLabel.font = UIFontMediumMake(15);
    [self.contentView addSubview:_nicknameLabel];
    [_nicknameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_avatarView.mas_right).offset(12);
        make.right.mas_equalTo(-76);
        make.top.equalTo(_avatarView);
    }];
    
    self.infoLabel = [[UILabel alloc] init];
    _infoLabel.font = UIFontMake(12);
    _infoLabel.textColor = HLTextColor2;
    _infoLabel.numberOfLines = 1;
    _infoLabel.text = @"27岁·西安·空乘";
    [self.contentView addSubview:_infoLabel];
    [_infoLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_nicknameLabel);
        make.bottom.equalTo(_avatarView);
    }];
    
    self.signLabel = [[UILabel alloc] init];
    _signLabel.textColor = HLTextColor1;
    _signLabel.font = UIFontMake(13);
    _signLabel.text = @"他还没有发现你的美！";
    [self.contentView addSubview:_signLabel];
    [_signLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_nicknameLabel);
        make.top.equalTo(_avatarView.mas_bottom).offset(12);
        make.right.mas_equalTo(-12);
    }];
    
    
    self.imagesView = [[HLHomeListImageView alloc] init];
    [self.contentView addSubview:_imagesView];
    [_imagesView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_signLabel);
        make.top.equalTo(_signLabel.mas_bottom).offset(12);
        make.height.mas_equalTo(112);
        make.right.mas_equalTo(-12);
        make.bottom.mas_equalTo(-12);
    }];
    
    self.greetButton = [[QMUIButton alloc] init];
    [_greetButton setImage:UIImageMake(@"home_cell_greet") forState:UIControlStateNormal];
    [_greetButton addTarget:self action:@selector(greetAction) forControlEvents:UIControlEventTouchUpInside];
    [_greetButton setTitle:@"搭讪" forState:UIControlStateNormal];
    [_greetButton setTitleColor:HLTextColor1 forState:UIControlStateNormal];
    _greetButton.cornerRadius = 14;
    _greetButton.layer.borderWidth = 1.5;
    _greetButton.layer.borderColor = HLTextColor1.CGColor;
    _greetButton.titleLabel.font = UIFontBoldMake(12);
    _greetButton.contentEdgeInsets = UIEdgeInsetsMake(2, 8, 2, 12);
    _greetButton.imagePosition = QMUIButtonImagePositionLeft;
    _greetButton.spacingBetweenImageAndTitle = 2;
    [self.contentView addSubview:_greetButton];
    _greetButton.hidden = YES;
    [_greetButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-12);
        make.centerY.equalTo(_avatarView);
    }];
    
    self.chatButton = [[QMUIButton alloc] init];
    [_chatButton setImage:UIImageMake(@"home_cell_chat") forState:UIControlStateNormal];
    [_chatButton addTarget:self action:@selector(chatAction) forControlEvents:UIControlEventTouchUpInside];
    [_chatButton setTitle:@"私信" forState:UIControlStateNormal];
    [_chatButton setTitleColor:HLTextColor3 forState:UIControlStateNormal];
    _chatButton.cornerRadius = 14;
    _chatButton.layer.borderWidth = 1.5;
    _chatButton.layer.borderColor = HLTextColor3.CGColor;
    _chatButton.titleLabel.font = UIFontBoldMake(12);
    _chatButton.contentEdgeInsets = UIEdgeInsetsMake(2, 8, 2, 12);
    _chatButton.imagePosition = QMUIButtonImagePositionLeft;
    _chatButton.spacingBetweenImageAndTitle = 2;
    [self.contentView addSubview:_chatButton];
    [_chatButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-12);
        make.centerY.equalTo(_avatarView);
    }];

    self.lineView = [[UIView alloc] init];
    _lineView.backgroundColor = HLLineColor;
    [self.contentView addSubview:_lineView];
    [_lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.right.mas_equalTo(0);
        make.height.mas_equalTo(0.5);
    }];
    
}

- (void)setModel:(HLHomeListModel *)model
{
    _model = model;
    
    _nicknameLabel.text = model.nickName;
    [_avatarView sd_setImageWithURL:[NSURL URLWithString:model.avatar] placeholderImage:[HLUIHelper avatarPlaceholder]];
    _infoLabel.text = [model info];
    _signLabel.text = model.selfSign;
    
    if (model.opuses.count > 0) {
        _imagesView.hidden = NO;
        _imagesView.opus = model.opuses;
        CGFloat width = (SCREEN_WIDTH - 76) / 3;
        [_imagesView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(width);
            make.top.equalTo(_signLabel.mas_bottom).offset(12);
        }];
    } else {
        _imagesView.hidden = YES;
        [_imagesView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(_signLabel.mas_bottom).offset(0);
            make.height.mas_equalTo(0);
        }];
    }
    
    [self.avatarBackView.layer removeAllAnimations];
    _avatarBackView.hidden = ![model.videoStatus boolValue];
    _videoButton.hidden = ![model.videoStatus boolValue];
    _avatarView.layer.borderColor =  [model.videoStatus boolValue] ? HLMainColor.CGColor : UIColorWhite.CGColor;
    
    if ([HLRunTime shareInstance].isFood) {
        _greetButton.hidden = YES;
        _chatButton.hidden = YES;
    } else {
        _greetButton.hidden = [model.chatUp boolValue];
        _chatButton.hidden = ![model.chatUp boolValue];
    }
    
}

- (void)deleteAnimateAvatarBackView
{
    [self.avatarBackView.layer removeAllAnimations];
}

- (void)animateAvatarBackView
{
    CABasicAnimation*posAni = [CABasicAnimation animationWithKeyPath:@"position"];
    posAni.toValue=[NSValue valueWithCGPoint:self.avatarBackView.center];
    CABasicAnimation*opcAni = [CABasicAnimation animationWithKeyPath:@"opacity"];
    opcAni.toValue=[NSNumber numberWithFloat:0.8];
    CABasicAnimation*bodAni = [CABasicAnimation animationWithKeyPath:@"bounds"];
    bodAni.toValue = [NSValue valueWithCGRect:CGRectMake(0, 0, 46, 46)];
    CAAnimationGroup *groupAni = [CAAnimationGroup animation];
    groupAni.animations = @[posAni,opcAni,bodAni];
    groupAni.duration = 0.6;
    groupAni.fillMode = kCAFillModeForwards;
    groupAni.removedOnCompletion = NO;
    groupAni.autoreverses = YES;
    groupAni.repeatCount = MAXFLOAT;
    groupAni.timingFunction = [CAMediaTimingFunction                 functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    [self.avatarBackView.layer addAnimation:groupAni forKey:@"groupAni"];
}

- (void)chatAction
{
    
    [HLUIHelper chatWithUserId:_model.userId controller:self.qmui_viewController];
}

- (void)greetAction
{
    self.greetButton.enabled = NO;
    MJWeakSelf
    [HLUIHelper greetWithParam:@{@"toMemberId":_model.userId} callBack:^(BOOL isSuccess, NSString * _Nonnull message) {
        weakSelf.greetButton.enabled = YES;
        if (isSuccess){
            weakSelf.model.chatUp = @"1";
            [weakSelf.qmui_tableView reloadData];
        } else {
            if (message.length > 0) {
                [QMUITips showWithText:message];
            }
        }
    }];
}

- (void)videoAction
{
    HLUserInfoModel *user = [[HLUserInfoModel alloc] init];
    user.userId = _model.userId;
    user.avatar = _model.avatar;
    user.videoType = [_model videoType];
    [HLUIHelper callWithUser:user];
}

@end
